/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: PinController.java
 * Author:   linyao
 * Date:     2016-6-24 下午3:45:18
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.controller.marketing;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.model.enums.PaymentMethodEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.system.service.BusinessLogServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 *  签约合同变更
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/marketing/changecontact/")
public class ChangeContactController extends BaseController {

	@Autowired
	private SignedServiceFacade signedServiceFacade;
	@Autowired
	private BusinessLogServiceFacade businessLogServiceFacade;
	
	/**
	 * 签约合同付款方式变更
	 * @return
	 */
	@RequestMapping("contactchange")
	@ResponseBody
	public AjaxRes contactchange(String params, Long signedId, String paymentMethod) {
		AjaxRes ar = getAjaxRes();
		User user = ShiroUtil.getCurrentUser();
		try {
			if(null == signedId){
				ar.setRes(0);
				ar.setResMsg("签约记录不存在!");
			}else{
				//认筹信息
				Response<Signed> resSigned = signedServiceFacade.findById(signedId);
				Signed signed = resSigned.getEntity();
				if(null != signed){
					Response<SignedDto> resSignedDto = signedServiceFacade.contactChange(signed, params, user, paymentMethod);
					if(resSignedDto.isSuccess()){
						ar.setRes(1);
						ar.setResMsg(resSignedDto.getResultMessage());
					}else{
						ar.setRes(0);
						ar.setResMsg(resSignedDto.getResultMessage());
					}
				}else{
					ar.setRes(0);
					ar.setResMsg("签约记录不存在!");
				}
			}
		} catch (Exception ex) {
			//新增业务操作异常记录
			businessLogServiceFacade.saveExceptionLog(user, "签约合同付款方式变更", params, JSONObject.toJSONString(ex));
			
			ex.printStackTrace();
			logger.error("签约合同付款方式变更失败", ex);
			ar.setRes(0);
			ar.setResMsg("签约合同付款方式变更失败: " + ex);
		}
		return ar;
	}
	
	@RequestMapping(value = "getPaymentMethod")
	public void getPaymentMethod(HttpServletRequest request, HttpServletResponse response) {
		JSONArray pmArray = new JSONArray();
		try {
			for (PaymentMethodEnum dt : PaymentMethodEnum.values()) {
				JSONObject row = new JSONObject();
				row.put("value", dt.getNo());
				row.put("name", dt.getDes());
				pmArray.add(row);
			}
			resultJson(response, pmArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 变更签约合同、应收款信息
	 * 1.重置当前签约应收款、已收款信息
	 * 2.更新签约合同金额
	 * 3.保存新的应收款信息
	 * 4.需要进行手工收款动作
	 * @return
	 */
	@RequestMapping("signeRepairData")
	@ResponseBody
	public AjaxRes signeRepairData(String params, Long signedId, String paymentMethod) {
		AjaxRes ar = getAjaxRes();
		User user = ShiroUtil.getCurrentUser();
		try {
			//认筹信息
			Response<Signed> resSigned = signedServiceFacade.findById(signedId);
			Signed signed = resSigned.getEntity();
			if(null != signed){
				Response<SignedDto> resSignedDto = signedServiceFacade.signeRepairData(signed, params, user, paymentMethod);
				if(resSignedDto.isSuccess()){
					ar.setRes(1);
					ar.setResMsg(resSignedDto.getResultMessage());
				}else{
					ar.setRes(0);
					ar.setResMsg(resSignedDto.getResultMessage());
				}
			}else{
				ar.setRes(0);
				ar.setResMsg("签约记录不存在!");
			}
		} catch (Exception ex) {
			
			//新增业务操作异常记录
			businessLogServiceFacade.saveExceptionLog(user, "签约信息变更", params, JSONObject.toJSONString(ex));
			
			logger.error("签约信息变更", ex);
			ar.setRes(0);
			ar.setResMsg("签约信息变更" + ex);
		}
		return ar;
	}
}
